Ext.ns('Wlanauth.user');

Wlanauth.user.AddFormPanel = Ext.extend(Wlanauth.BaseFormPanel,{
	title:'用户资料新增',
	baseCls:'x-plant',
	iconCls:'user_add',
	constructor:function(config){
		Ext.apply(this,config);
		
		var formID = new Date().getTime();
		
		var ipReg = /^(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)\.(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])$/;
		
	 	var compositefield = new Ext.form.CompositeField({
	 		fieldLabel:'所需费用',
			combineErrors: false,
			items: [{
            	xtype: 'displayfield',width:80,value:'1',cls:'x-field-display'
            },{
            	xtype: 'displayfield',value: 'M * '
            },{
            	xtype: 'displayfield',name:'radratio',value:'loading...',width:40,cls:'x-field-display'
            },{
            	xtype: 'displayfield',value:'KB/元  = '
            },{
            	xtype: 'displayfield',readOnly:true,
            	width: 100,allowBlank: true,value:'   '
            },{
            	xtype: 'displayfield',value: '元'
            }]
	 	});
	 	//基本信息
	 	var commonInfoPanel = new Ext.form.FieldSet({
        	title:'基本资料',
        	buttonAlign:'center',
        	width:500,
        	defaults:{xtype:'textfield',layout:'form',labelWidth:120,anchor:'90%'},
        	items:[{
        		fieldLabel:'绑定IP',name:'entity.bindIP',allowBlank:false,maxLength:25,regex:ipReg,regexText:'请输入正确的IP地址',
        			listeners:{
	        			'change':function(f,newVal,oldVal){
	        				var nextF = f.nextSibling();
	        				var nextFOldV = nextF.getValue();
	        				nextF.setValue(newVal);
	        				nextF.fireEvent('change',nextF,newVal,nextFOldV);
	        			}
        			}
        		},{
        			fieldLabel:'卡号',name:'entity.username',allowBlank:false,maxLength:25,
        			xtype:'keytextfield',
        			url:'user!list.action?dataOnly=true'
        		},{
        			fieldLabel:'PUK码',name:'entity.puk',value:'111111',allowBlank:false,maxLength:50
//        			listeners:{
//        				'change':function(f,newV,oldV){
//        					var nextF = f.nextSibling();
//        					nextF.setValue(newV);
//        				}
//        			}
        		},{
        			fieldLabel:'密码',name:'entity.password',value:'111',allowBlank:false,xtype:'hidden'
        		},{
        			fieldLabel:'有效期',name:'entity.expiredTime',value:'2099-01-01 00:00:00',xtype:'datefield',format:'Y-m-d',allowBlank:false
        		},{
        			fieldLabel:'总流量(MB)',name:'entity.maxOctets',allowBlank:false,
        			xtype:'localcombo',data:[['1G卡','1024'],['2G卡','2048'],['3G卡','3072']],editable:true,
        			listeners:{
        				'change':function(field,newVal,oldVal){
        					var items = compositefield.items;
        					items.itemAt(0).setValue(newVal);
        					
        					var radRatioCmp = items.itemAt(2);
        					if(radRatioCmp.inited){
        						var val = Math.round((newVal*radRatioCmp.getValue())*100)/100;
        						items.itemAt(4).setValue(val);
        						compositefield.nextSibling().setValue(val)
        					}
     
        				}
        			}
        	 },
        	 //compositefield,
        	 {
				fieldLabel:'总金额(元)',
				xtype:'numberfield',
				width:320,
				allowBlank:false,
				name:'entity.actualFee'
    		}],
    		buttons:[{
	        	text:'新增',
	        	iconCls:'add',
	        	handler:this.doSubmit,
	        	scope:this
	        },{
	        	text:'取消',
	        	iconCls:'cancel',
	        	handler:function(){
	        		ZZExt.ApplicationContext.removeTabCmp(this);
	        	},
	        	scope:this
	        }]
        });
	 	
	 	Wlanauth.user.AddFormPanel.superclass.constructor.call(this,{
			actionClass:'user',
			buttonAlign:'center',
			waitMsgTarget: true,
	        padding:5,
	        width:500,
	        items:[commonInfoPanel]
     	});
	 	
	 	this.on('actionSuccess',function(){
	 		//var radRatioCmp = compositefield.items.itemAt(2);
	 		var _this = this;
	 		
	 		Ext.Msg.confirm('提示','操作成功,数据已保存! 是否继续添加?',function(btn){
	 			if(btn=='yes'){
	 				//var radRatio = radRatioCmp.getValue();
	 		 		_this.getForm().reset();
	 		 		//radRatioCmp.setValue(radRatio);
	 			} else {
	 				ZZExt.ApplicationContext.removeTabCmp(_this);
	 				var tab = ZZExt.ApplicationContext.openTab('wlanauth.user.tabpanel');
	 				tab.tabPanel.items.itemAt(0).getStore().reload();
	 			}
	 		});
	 	  
	 	},this);
	 	
	 	/*
	 	 * 取消比率
	 	 * Ext.Ajax.request({
	 		async: false, 
	 		url:'systemProperty!findKey.action?systemProperty.key=RADIUS_RATIO',
	 		success:function(response,opts){
	 			var r = Ext.decode(response.responseText);
	 			if(r.success){
	 				var radRatioCmp = this.getForm().findField('radratio');
	 				radRatioCmp.setValue(r.data.value);
	 				radRatioCmp.inited=true;
	 			}
	 		},
	 		scope:this
	 	});*/
	},
	isValid:function(form){
		var flag = form.isValid();
		if(!flag) return false;
		var oct = form.findField('entity.maxOctets',false).getValue();
		var reg = /^\d+$/;
		if(oct==null||!reg.test(oct)||oct<1){
			Ext.Msg.alert('提示','总流量必须为大于或等于1的整型数字');
			flag = false;
		}
		return flag;
	}
});

Ext.reg('wlanauth.user.addformpanel',Wlanauth.user.AddFormPanel);